package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Jonas Dietz and Ramon Victor from the Cologne University of Applied Sciences
 * @since 2012
 * This Class contains all neccesary information about an IM-Profile
 */
public class Profile 
{
    private String protocol;
    private String user;
    private char[] password;
    private String server;
    private String domain;
    private int port;
    private String security;
    private char[] dummy;

    private char[] empty = new char[1];

    public Profile()
    {
	this.setProtocol("");
	this.setUser("");
	this.setPassword(dummy);
	this.setServer("");
	this.setDomain("");
	this.setPort(0);
	this.setSecurity("");
    }

    /**
     * @param protocol	specifies the IM-Protocol used by this Profile
     * @param user	represents the username
     * @param password	corresponding password
     * @param server	the Server belonging to the protocol
     * @param domain	target Domain for the connection
     * @param port	port used by the Protocol
     * @param security	dummy, so far...
     */
    public Profile(String protocol, String user, char[] password, String server, String domain, int port, String security)
    {
	this.setProtocol(protocol);
	this.setUser(user);
	this.setServer(server);
	this.setPort(port);
	this.setSecurity(security);	
	this.setDomain(domain);
	this.setPassword(password);
    }

    public String getDomain(){
	return domain;
    }

    public char[] getPassword() {
	return password;
    }

    public int getPort() {
	return port;
    }

    /*
     * The following bloc contains the typical get & set methods
     */
    public String getProtocol() {
	return protocol;
    }

    public String getSecurity() {
	return security;
    }

    public String getServer() {
	return server;
    }

    public String getUser() {
	return user;
    }

    /**
     * This Method loads the Profile specified in the parameter "name"
     * @param name	Profile to be loaded
     * @return		Profile corresponding to the name or null
     */
    public Profile loadProfile(String name)
    {
	String fileName = "" + name + ".jimp";
	int i = 0;
	Profile profile = new Profile();

	try {
	    BufferedReader in = new BufferedReader(new FileReader(fileName));
	    String line = null;
	    while ((line = in.readLine()) != null) {
		switch(i)
		{
		case 0: profile.setProtocol(line); i++; break;
		case 1: profile.setUser(line); i++; break;
		case 2: profile.setPassword(line.toCharArray()); i++; break;
		case 3: profile.setServer(line); i++; break;
		case 4: profile.setDomain(line); i++; break;
		case 5: profile.setPort(Integer.parseInt(line)); i++; break;
		case 6: profile.setSecurity(line); break;
		}
	    }
	    in.close();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return profile;
    }

    /**
     * This method saves the current Profile Object into a textfile with a specific scheme
     */
    public void saveProfile()
    {
	FileWriter writer;
	File file;

	String fileName = "" + getProtocol() + "_" + getUser() + ".jimp";	
	try {
	    file = new File(fileName);
	    writer = new FileWriter(file);
	    writer.write(getProtocol());
	    writer.write(System.getProperty("line.separator"));
	    writer.write(getUser());
	    writer.write(System.getProperty("line.separator"));
	    if(getPassword().equals(empty)){
		writer.write("");
		writer.write(System.getProperty("line.seperator"));
	    }
	    else{
		writer.write(getPassword());
		writer.write(System.getProperty("line.separator"));		
	    }
	    writer.write(getServer());
	    writer.write(System.getProperty("line.separator"));
	    writer.write(getDomain());
	    writer.write(System.getProperty("line.separator"));
	    writer.write("" + getPort());
	    writer.write(System.getProperty("line.separator"));
	    writer.write(getSecurity());
	    writer.flush();
	    writer.close();		
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    public void setDomain(String domain) {
	this.domain = domain;
    }

    public void setPassword(char[] password) {
	this.password = password;
    }

    public void setPort(int port) {
	this.port = port;
    }

    public void setProtocol(String protocol) {
	this.protocol = protocol;
    }

    public void setSecurity(String security) {
	this.security = security;
    }

    public void setServer(String server) {
	this.server = server;
    }

    public void setUser(String user) {
	this.user = user;
    }
}
